package service;

import dao.BillMapper;
import dao.ProviderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pojo.Provider;

import java.sql.Connection;
import java.util.List;
@Service
@Transactional
public class ProviderServiceImpl implements ProviderService {

    @Autowired
    private ProviderMapper providerMapper;
    @Autowired
    private BillMapper billMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Provider> getProviderList() {
        return providerMapper.getProviderList();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Provider> getProvier(String proCode, String proName) {
        return providerMapper.getProvier(proCode,proName);
    }

    @Override
    public boolean add(Provider provider) {
        return providerMapper.add(provider) > 0;
    }
    /**
     * 业务：根据ID删除供应商表的数据之前，需要先去订单表里进行查询操作 若订单表中无该供应商的订单数据，则可以删除
     * 若有该供应商的订单数据，则不可以删除 返回值billCount 1> billCount == 0 删除---1 成功 （0） 2 不成功
     * （-1） 2> billCount > 0 不能删除 查询成功（0）查询不成功（-1）
     *
     * ---判断 如果billCount = -1 失败 若billCount >= 0 成功
     */
    @Override
    public int deleteProviderById(String delId) {
        Connection connection = null;
        int billCount = billMapper.getBillCountByProviderId(delId);
        if (billCount == 0) {
            providerMapper.deleteProviderById(delId);
        }

        return billCount;
    }

    @Override
    public Provider getProviderById(String id) {
        return providerMapper.getProviderById(id);
    }

    @Override
    public boolean modify(Provider provider) {
        return providerMapper.modify(provider) > 0;
    }
}
